package org.jsping.music;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsping.entity.Music;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 根据传递过来的页数获取对应页数的列表
 */
@WebServlet("/PageMusic")
public class PageMusic extends HttpServlet {
	private static final long serialVersionUID = 1L;
     
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取传递过来的页数
		int page = Integer.valueOf(request.getParameter("page"));
		//根据页数获取数据
		Map<String, Music> musics = CacheMusic.getInstance().getMusic(page);
		//转成json
		JSONObject itemJSONObj = JSONObject.parseObject(JSON.toJSONString(musics));
		PrintWriter pw = response.getWriter();
		pw.println(itemJSONObj);
		pw.flush();
		pw.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
